package com.situ.user.controller;

import com.situ.user.pojo.User;
import com.situ.user.service.IUserService;
import com.situ.user.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.List;

//  @Controller
//  @Controller + @ResponseBody
@RestController
// @RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /*// /user/selectById?id=1
    @RequestMapping("/selectById")
    @ResponseBody
    public User selectById(Integer id) {

    }*/

    //  /localhost:8081/user/1
    // @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @GetMapping("/user/{id}")
    public User selectById(@PathVariable("id") Long id)  {
        System.out.println("id: " + id);
       /* User user = new User();
        user.setId(1L);
        user.setName("张三");
        user.setAddress("北京");*/

        User user = userService.selectById(id);
        // return JSONResult.ok(user);
        return user;
    }

    // /localhost:8081/user
    @GetMapping("/user")
    public JSONResult selectAll() {
        List<User> list = userService.selectAll();
        return JSONResult.ok(list);
    }

    // /localhost:8081/user/1    method=DELETE
    @DeleteMapping("/user/{id}")
    public JSONResult deleteById(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.ok("删除成功");
    }

    // @RequestBody主要用来接收前端传递给后端的json字符串中的数据的
    @PostMapping("/user")
    public JSONResult add(@RequestBody User user) {
        userService.add(user);
        return JSONResult.ok("添加成功");
    }

    @PutMapping("/user/{id}")
    public JSONResult add(@PathVariable("id")Long id) {
        return JSONResult.ok("添加成功");
    }
}
